//
//  MineViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/3.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "MineViewController.h"
#import "UIView+Utils.h"

//Controller
#import "PersonInfoViewController.h"
#import "SetViewController.h"
#import "ShopGoodsDetailVC.h"

#import "OrderListViewController.h"
#import "ServeCollectionViewController.h"
#import "ShopAttentionViewController.h"
#import "MyFootprintViewController.h"

#import "WalletViewController.h"
#import "MyIntegralViewController.h"
#import "MyCommissionViewController.h"
#import "CouponListViewController.h"
#import "MyBillViewController.h"
#import "AddressManageViewController.h"
#import "ShopManageViewController.h"
#import "FeedbackViewController.h"

#import "SelectBoxModelController.h"
#import "OrderPaperBoardViewController.h"

#import "NeedCenterViewController.h"

//Cell
#import "MineHeadTableViewCell.h"
#import "MyOperationCell.h"
#import "MineClassCell.h"
#import "OtherOprerationCell.h"
#import "MayLikeTableViewCell.h"
#import "MayLikeModel.h"
#import "CustomDemandListModel.h"
#import "FPDesignCenterHomeCell.h"

@interface MineViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView     *tableView;
@property (nonatomic, strong) NSMutableArray    *mayLikeData;
@property (nonatomic, strong) NSDictionary      *headDic;
@property (nonatomic, assign) NSInteger         userType;
@property (nonatomic, strong) NSMutableArray    *demandData;

@end

@implementation MineViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _mayLikeData = [[NSMutableArray alloc] init];
    // Do any additional setup after loading the view.
    self.headDic = [NSDictionary dictionary];
    self.demandData = [NSMutableArray array];
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, -STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT + STATUS_BAR_HEIGHT - TAB_BAR_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:self.tableView];
    [self getData];
}

- (void)getData {
    NSString *url = [NSString stringWithFormat:@"%@app/user_info",REQUESTHEADER];
    NSDictionary *dict = @{@"token":[FPLoginAndRegisterTools getToken]};
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            self.userType = [successResponse[@"data"][@"type"] integerValue];
            if (self.userType == 0) {
                [self getDemandsData];
            }else{
                [self getMayLikeData];
            }
            self.headDic = successResponse[@"data"];
            [self.tableView reloadData];
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

#pragma mark - UITableViewDelegate,DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 4 && self.userType == 1) {
        return self.demandData.count;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        MineHeadTableViewCell *cell = [MineHeadTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
        if (_headDic.allKeys.count > 0) {
            cell.dict = _headDic;
        }
        cell.infoClick = ^{
            PersonInfoViewController *vc = [[PersonInfoViewController alloc] init];
            vc.userType = self.userType;
            [self.navigationController pushViewController:vc animated:YES];
        };
        cell.setClick = ^{
            SetViewController *vc = [[SetViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        };
        cell.indexClick = ^(NSInteger index) {
            if (index == 0) {
                OrderListViewController *vc = [[OrderListViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index == 1) {
                ServeCollectionViewController *vc = [[ServeCollectionViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index == 2) {
                ShopAttentionViewController *vc = [[ShopAttentionViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index == 3) {
                MyFootprintViewController *vc = [[MyFootprintViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
        };
        return cell;
    }else if (indexPath.section == 1) {
        MyOperationCell *cell = [MyOperationCell cellWithTableView:tableView andIndexPath:indexPath];
        return cell;
    }else if (indexPath.section == 2) {
        MineClassCell *cell = [MineClassCell cellWithTableView:tableView andIndexPath:indexPath];
        cell.indexClick = ^(NSInteger index) {
            if (index == 0) {
                WalletViewController *vc = [[WalletViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index == 1) {
                MyIntegralViewController *vc = [[MyIntegralViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index == 2) {
                MyCommissionViewController *vc = [[MyCommissionViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index == 3) {
                CouponListViewController *vc = [[CouponListViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index == 4) {
                MyBillViewController *vc = [[MyBillViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index == 5) {
                AddressManageViewController *vc = [[AddressManageViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index == 6) {
                ShopManageViewController *vc = [[ShopManageViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
                
//                ShopGoodsDetailVC *goodsDetailVC = [[ShopGoodsDetailVC alloc] init];
//                //将商品ID传入下个界面
//                [self.navigationController pushViewController:goodsDetailVC animated:true];
                
            }else if (index == 7) {
                FeedbackViewController *vc = [[FeedbackViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index == 8) {
                
            }
        };
        return cell;
    }else if (indexPath.section == 3) {
        OtherOprerationCell *cell = [OtherOprerationCell cellWithTableView:tableView andIndexPath:indexPath];
        cell.seeClick = ^{
            NeedCenterViewController *vc = [[NeedCenterViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        };
        return cell;
    }else {
        if (self.userType == 1) {
            FPDesignCenterHomeCell *cell = [FPDesignCenterHomeCell cellWithTableView:tableView andIndexPath:indexPath andCellHeight:ScreenX375(80)];
            cell.customDemandListModel = [self.demandData objectAtIndex:indexPath.row];
            return cell;
        }else{
            MayLikeTableViewCell *cell = [MayLikeTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
            cell.mayLikeArray = self.mayLikeData;
            cell.indexClick = ^(NSInteger index) {
                MayLikeModel *model = [self.mayLikeData objectAtIndex:index];
                if ([model.goods_type integerValue] == 1) {
                    SelectBoxModelController *vc = [[SelectBoxModelController alloc] init];
                    vc.producId = model.product_id;
                    vc.isPaperBox = YES;
                    [self.navigationController pushViewController:vc animated:YES];
                }else if ([model.goods_type integerValue] == 2) {
                    OrderPaperBoardViewController *vc = [[OrderPaperBoardViewController alloc] init];
                    vc.productId = model.product_id;
                    [self.navigationController pushViewController:vc animated:YES];
                }
            };
            return cell;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            return ScreenX375(250) + STATUS_BAR_HEIGHT;
            break;
        case 1:
            return ScreenX375(64);
            break;
        case 2:
            return ScreenX375(245);
            break;
        case 3:
            return ScreenX375(84);
            break;
        case 4:
            if (_userType == 1) {
                return ScreenX375(80);
            }else{
                return ScreenX375(395);
            }
            break;
        default:
            return 0;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 4) {
        return 0.01;
    }
    return ScreenX375(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 4) {
        return nil;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(10))];
    view.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
    return view;
}

-(void)getMayLikeData{
    NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"factory_id":kNSUDefaultReadKey(@"FactoryId")}];
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/guess_you_like",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSError *error = nil;
            for(NSDictionary *dict in successResponse[@"data"]){
                MayLikeModel *model = [[MayLikeModel alloc] initWithDictionary:dict error:&error];
                [self.mayLikeData addObject:model];
            }
            [self.tableView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

-(void)getDemandsData{
    NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"token":kNSUDefaultReadKey(@"token")}];
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/customer_demand",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSError *error = nil;
            for(NSDictionary *dict in successResponse[@"data"]){
                CustomDemandListModel *model = [[CustomDemandListModel alloc] initWithDictionary:dict error:&error];
                [self.demandData addObject:model];
            }
            [self.tableView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

@end
